﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using X3DXNA.interfaces;
using X3DXNA.basicModels;

namespace X3DXNA.core.Boundings
{
    #region BoundingBox

    public class MyBoundingBox : IBounding
    {
        BoundingBox boundingBox;
        Vector3[] corners;

        public MyBoundingBox(BoundingBox boundingBox)
        {
            this.boundingBox = boundingBox;
            corners = this.boundingBox.GetCorners();
        }

        public Vector3[] GetCorners()
        {
            return boundingBox.GetCorners();
        }

        public bool Intersects(BoundingFrustum frustum)
        {
            return boundingBox.Intersects(frustum);
        }

        public float? Intersects(Ray ray)
        {
            return (boundingBox.Intersects(ray));
        }

        public void tranformBounding(Matrix world)
        {
            Vector3[] newCorners = new Vector3[corners.Length];

            for (int i = 0; i < corners.Length; i++)
            {
                newCorners[i] = Vector3.Transform(corners[i], world);
            }

            this.boundingBox = BoundingBox.CreateFromPoints(newCorners);
        }
    }

    #endregion

    #region BoundingSphere

    public class MyBoundingSphere : IBounding
    {
        BoundingSphere boundingSphere, originalBoundingSphere;

        public MyBoundingSphere(BoundingSphere boundingSphere)
        {
            this.boundingSphere = boundingSphere;
            this.originalBoundingSphere = boundingSphere;
        }

        public Vector3 Center()
        {
            return boundingSphere.Center;
        }

        public float Radius()
        {
            return boundingSphere.Radius;
        }

        public bool Intersects(BoundingFrustum frustum)
        {
            return boundingSphere.Intersects(frustum);
        }

        public float? Intersects(Ray ray)
        {
            return (boundingSphere.Intersects(ray));
        }

        public void tranformBounding(Matrix world)
        {
            this.boundingSphere = this.originalBoundingSphere.Transform(world);
        }
    }

    #endregion
}
